#include "playscene.h"

#include<QDebug>
#include<QMenuBar>
#include<QPainter>
#include<QLabel>
#include"mypushbutton.h"
#include<QTimer>
#include"mycoin.h"
#include<dataconfig.h>
#include<QDebug>
#include<QPropertyAnimation>
#include<QSound>

//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{

//}
PlayScene::PlayScene(int levelNum){

    //显示关卡数
    QString str=QString("selet %1").arg(levelNum);
    qDebug()<<str;
    this->levelIndex=levelNum;

    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));

    //创建菜单栏
    QMenuBar *bar=menuBar();
    setMenuBar(bar);
    QMenu *starMenu=bar->addMenu("now");
    QAction *quitAction=starMenu->addAction("tuichu");

    //关闭页面
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //添加音效资源
    //返回按钮音效
    QSound *backSound = new QSound(":/res/BackButtonSound.wav",this);
    //翻金币音效
    QSound *flipSound = new QSound(":/res/ConFlipSound.wav",this);
    //胜利按钮音效
    QSound *winSound = new QSound(":/res/LevelWinSound.wav",this);


    //关闭按钮
    MyPushButton *closeBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    closeBtn->setParent(this);
    closeBtn->move(this->width()-closeBtn->width(),this->height()-closeBtn->height());

    //点击返回按钮，把自己隐藏，而后向第一个页面发送一个信号，让第一个页面把自己打开。
    connect(closeBtn,&MyPushButton::clicked,[=](){

        //点击返回按钮播放音效
        backSound->play();

        //延时发送信号实现场景切换
        QTimer::singleShot(500,this,[=](){
            this->hide();
            emit this->chooseSceneBack();
        });

    });

    //显示关卡数
    QLabel *label=new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);
    QString str2=QString("Leavel:%1").arg(this->levelIndex);
    label->setText(str2);
    label->setGeometry(QRect(30,this->height()-50,120,50));

    dataConfig config;
    //初始化每个关卡的二维数组
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            this->gameArray[i][j]=config.mData[this->levelIndex][i][j];
        }
    }

    //胜利图片实现
    QLabel* winLabel = new QLabel;
        QPixmap tmpPix;
        tmpPix.load(":/res/LevelCompletedDialogBg.png");
        winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
        winLabel->setPixmap(tmpPix);
        winLabel->setParent(this);
        winLabel->move( (this->width() - tmpPix.width())*0.5 , -tmpPix.height());



    //创建金币的背景图片
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            QLabel *label=new QLabel;
            label->setGeometry(0,0,50,50);
            label->setPixmap(QPixmap(":/res/BoardNode(1).png"));
            label->setParent(this);
            label->move(57 + i*50,200+j*50);

            //创建金币
            QString str1;
            if(this->gameArray[i][j]==1){
                str1=":/res/Coin0001.png";
            }
            else{
                 str1=":/res/Coin0008.png";
            }
            MyCoin *coin=new MyCoin(str1);
            coin->setParent(this);
            coin->move(59 + i*50,204+j*50);

            //金币的坐标和属性（正反面）
            coin->posx=i;
            coin->posy=j;
            coin->flag=this->gameArray[i][j];

            //将金币放入到金币的二维数组里，以便之后进行维护
            coinBtn[i][j]=coin;

            //点击金币进行翻转
            connect(coin,&MyCoin::clicked,[=](){

                //反金币音效
            flipSound->play();
                //点击按钮，将所有的按钮先都禁用
                for(int i=0;i<4;i++){
                    for(int j=0;j<4;j++){
                        this->coinBtn[i][j]->isWin=true;
                    }
                }

                coin->changeFlag();
                //er二维数组的维护
                this->gameArray[i][j]=this->gameArray[i][j]==0?1:0;

                //反转周围的金币
                QTimer::singleShot(300,this,[=](){

                if(coin->posx+1<=3){//翻转右侧金币的条件
                    coinBtn[coin->posx+1][coin->posy]->changeFlag();
                    this->gameArray[coin->posx+1][coin->posy]=this->gameArray[coin->posx+1][coin->posy]==0?1:0;


                }
                //周围左侧的硬币
                if(coin->posx-1>=0){
                    coinBtn[coin->posx-1][coin->posy]->changeFlag();
                    this->gameArray[coin->posx-1][coin->posy]=this->gameArray[coin->posx-1][coin->posy]==0?1:0;

                }

                if(coin->posy+1<=3){//翻转上侧金币的条件
                    coinBtn[coin->posx][coin->posy+1]->changeFlag();
                    this->gameArray[coin->posx][coin->posy+1]=this->gameArray[coin->posx][coin->posy+1]==0?1:0;


                }
                if(coin->posx+1<=3){//翻转下侧金币的条件
                    coinBtn[coin->posx][coin->posy-1]->changeFlag();
                    this->gameArray[coin->posx][coin->posy-1]=this->gameArray[coin->posx][coin->posy-1]==0?1:0;


                }

                //翻完周围的金币后 将所有的金币解开禁用
                for(int i=0;i<4;i++){
                    for(int j=0;j<4;j++){
                        this->coinBtn[i][j]->isWin=true;
                    }
                }

                //判断是否胜利
                this->isWin=true;
                for(int i=0;i<4;i++){
                    for(int j=0;j<4;i++){
                        if(coinBtn[i][j]->flag==false){//只要有一个反面就不算胜利
                            this->isWin=false;
                            break;
                        }
                    }
                }
                if(this->isWin==true){

                    //胜利的音效
                    winSound->play();
                qDebug()<<"WIN";
                //将所有按钮胜利的标志改为true
                //如果再次点击按钮，直接return.
                for(int i=0;i<4;i++){
                    for(int j=0;j<4;i++){
                        coinBtn[i][j]->isWin=true;
                    }
                }
                //将胜利的图片砸下来
                QPropertyAnimation *animation=new QPropertyAnimation(winLabel,"geomtry");
                //设置时间间隔
                animation->setDuration(1000);
                //设置开始位置
                animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));

                //设置结束位置
                  animation->setEndValue((QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height())));

                //设置缓和曲线
                  animation->setEasingCurve(QEasingCurve::OutBounce);

                  animation->start();
                }




                 });
            });
        }
    }
}





void PlayScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    painter.drawPixmap(0,0,QPixmap(":/res/PlayLevelSceneBg.png"));

    QPixmap pix;
    pix.load(":/res/Title.png");
    //缩放图片
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix.width(),pix.height(),pix);

}
